import numpy as np
from sklearn.ensemble import RandomForestRegressor
from sklearn import metrics
import matplotlib.pyplot as plt # plt 用于显示图片

x_data= np.load('x_data.npy')
y_data= np.load('y_data.npy')
localtion= np.load('LOCATION.npy')
num_train=range(0,9)
num_test=range(9,20)
for i in num_train:
    #print(x_data[i].shape)
    if i==num_train[0]:
        x_train=x_data[i]
        y_train = y_data[i]
    else:
        x_train=np.concatenate((x_train,x_data[i]), axis=0)
        y_train=np.concatenate((y_train, y_data[i]), axis=0)


print('训练数据大小',x_train.shape)

for i in num_test:
    #print(x_data[i].shape)
    if i==num_test[0]:
        x_test = x_data[i]
        y_test = y_data[i]
    else:
        x_test=np.concatenate((x_test, x_data[i]), axis=0)
        y_test=np.concatenate((y_test, y_data[i]), axis=0)

print('测试数据大小',x_test.shape)

rf = RandomForestRegressor(n_estimators=1000)  # 这里使用了默认的参数设置
rf.fit(x_train, y_train)  # 进行模型的训练

y_predict = rf.predict(x_test)
y_predict [y_predict  >= 0.5] = 1
y_predict [y_predict  < 0.5] = 0

print('总体准确度',metrics.accuracy_score(y_test, y_predict))
np.save('Predict.npy', y_predict)

for i in num_test:
    x_test= x_data[i]
    y_test= y_data[i]
    loc=localtion[i]
    predict_img=np.zeros((584,565,3))
    #print('测试数据大小',x_test.shape)
    #print('测试数据大小2', loc.shape)
    y_predict = rf.predict(x_test)
    y_predict [y_predict  >= 0.5] = 1
    y_predict [y_predict  < 0.5] = 0

    print('图片'+str(i),metrics.accuracy_score(y_test, y_predict))
    for j in range(len(loc)):
        if y_predict[j]==0:
            predict_img[int(loc[j, 0]), int(loc[j, 1]), 2] = 1#静脉为0 蓝色
        else:
            predict_img[int(loc[j, 0]), int(loc[j, 1]), 0] = 1#动脉为1 红色

    plt.imshow(predict_img);plt.axis('off');plt.show()  # 显示灰度图像







